using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Aga.Psad.Backend.Repositories;
using AutoMapper;
using Aga.Psad.Backend.Entities;
using Aga.Psad.Mvc.Models.Despachos;
using Aga.Psad.Mvc.Models.Clientes;
using Aga.Psad.Mvc.Helpers;
using System.IO;
using Aga.Psad.Common;
using System.Web.Security;

namespace Aga.Psad.Mvc.Controllers
{
    [Authorize(Roles="Administrador")]
    public class DespachosController : ControllerBase
    {

        public DespachoRepository Despachos { get; private set; }
        public ClienteRepository Declarantes { get; private set; }
        public ClienteDeClienteRepository Importadores { get; private set; }
        
        public DespachosController()
        {
            this.Despachos = new DespachoRepository();
            this.Declarantes = new ClienteRepository();
            this.Importadores = new ClienteDeClienteRepository();

            InitializeMappers();
        }

        private void InitializeMappers()
        {
            Mapper.CreateMap<Despacho, DespachoViewModel>();
            Mapper.CreateMap<DespachoViewModel, Despacho>();
            Mapper.CreateMap<Cliente, ClienteViewModel>();
            Mapper.CreateMap<ClienteViewModel, Cliente>();
            Mapper.CreateMap<ClienteDeCliente, ClienteDeClienteViewModel>();
            Mapper.CreateMap<ClienteDeClienteViewModel, ClienteDeCliente>();
        }
        
        public ActionResult Index()
        {
            try
            {
                var despachos = Despachos.GetAll();

                var model = Mapper.Map<List<Despacho>,List<DespachoViewModel>>(despachos);

                return View(model);

            }
            catch (Exception ex)
            {
                return ShowError(ex);
            }
        }

        public ActionResult Details(int id)
        {
            try
            {
                var despacho = Despachos.GetById(id);

                var model = Mapper.Map<Despacho, DespachoViewModel>(despacho);

                return View(model);

            }
            catch (Exception ex)
            {
                return ShowError(ex);
            }
        }

        public ActionResult Create()
        {
            try
            {
                DespachoViewModel model = new DespachoViewModel
                {
                    Creado = DateTime.Now
                };

                var declarantes = Declarantes.GetAll().Select(d => new SelectListItem { Text = d.RazonSocial, Value = d.Id.ToString() });
                model.Declarantes.AddRange(declarantes);

                return View(model);

            }
            catch (Exception ex)
            {
                return ShowError(ex);
            }
        } 

        //
        // POST: /Despachos/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Despachos/Edit/5
 
        public ActionResult Edit(int id)
        {
            try
            {
                return View();

            }
            catch (Exception ex)
            {
                return ShowError(ex);
            }
        }

        //
        // POST: /Despachos/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public void Echo()
        {
            XmlHelper helper = new XmlHelper();
            StreamReader reader = new StreamReader(HttpContext.Request.InputStream);
            string xml = reader.ReadToEnd();
            

        }
    }
}
